﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BLL;

namespace GMS
{
    public partial class Pages_Estimate : System.Web.UI.Page
    {
        static Control msg = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadStoneDetails();

                LoadCostDetails();

                rptrStoneCategory.DataSource = Common.GetStoneTypeCategoryWithPrice(SessionVariable.Instance.UserDetails.SelectedStoneTypeId);
                rptrStoneCategory.DataBind();

                SendEmail(SendMail.ADMIN_EMAILS_CC);
            }
            
        }

        private bool SendEmail(string toWhom)
        {
         bool IsMailSuccess = BLL.SendMail.SendEmailNotification(
                toWhom,
                string.Format(Utility.GetEstimateEmailTemplate("template_mail_estimate.txt"),
                SessionVariable.Instance.UserDetails.Name,
                SessionVariable.Instance.UserDetails.UserId,
                SessionVariable.Instance.UserDetails.Cost.TotalPrice, 
                SessionVariable.Instance.UserDetails.PhoneNumber,
                SessionVariable.Instance.UserDetails.Email,
                SessionVariable.Instance.UserDetails.Options.ProjectTypeId,
                SessionVariable.Instance.UserDetails.SelectedStoneTypeId,
                SessionVariable.Instance.UserDetails.Cost.Surface
                ),
                false);

         return IsMailSuccess;
        }

        private void LoadStoneDetails()
        {
            DAL.Stone s = BLL.Common.GetStoneById(SessionVariable.Instance.UserDetails.Options.StoneId);
            DAL.StoneType sType = BLL.Common.GetStoneTypeById(SessionVariable.Instance.UserDetails.SelectedStoneTypeId);
            DAL.StoneCategory sCategory = BLL.Common.GetStoneCategoryById(SessionVariable.Instance.UserDetails.SelectedStoneCategoryId);

            if (s != null)
            {
                lblMaterial.Text = s.Name;
                lblOrigin.Text = s.Origin;
                lblThickness.Text = s.Thickness;
                imgStoneImage.ImageUrl = s.ImagePath;
                imgStoneImage.AlternateText = s.ImagePath;
                imgStoneImage.ToolTip = s.ImagePath;
            }

            if (sType != null)
            {
                lblStoneType.Text = sType.Name;
            }

            if (sCategory != null)
            {
                lblPriceGroup.Text = sCategory.Name;
            }
        }

        private void LoadCostDetails()
        {
            lblUserId.Text = String.Concat("GMW", Convert.ToString(SessionVariable.Instance.UserDetails.UserId));

            lblSurface.Text = SessionVariable.Instance.UserDetails.Cost.Surface.ToString("F2");
            lblSurfaceDollars.Text = (SessionVariable.Instance.UserDetails.Cost.Surface * 
                                       SessionVariable.Instance.UserDetails.Cost.SurfacePrice).ToString("F2");

            lblBS.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.Backsplash);
            
            // changed to surface price as per requirement 1.21.12 meeting

            lblBSDollars.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.Backsplash *
                SessionVariable.Instance.UserDetails.Cost.SurfacePrice);

            DAL.Edge e = BLL.Common.GetEdgeById(SessionVariable.Instance.UserDetails.Options.EdgeId);
            if (e != null)
            {
                lblEdgeType.Text = string.Concat(e.Name, " Edge: ");
            }
            else
            {
                lblEdgeType.Text = "Edge: ";
            }

            lblEdgeLength.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.Edge);
            lblEdgeDollars.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.Edge *
                SessionVariable.Instance.UserDetails.Cost.EdgePrice);

            lblSinkCutOutsQt.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.Sinkout);
            lblSinkCutDollars.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.Sinkout *
                SessionVariable.Instance.UserDetails.Cost.SinkoutPrice);

            lblZipDistance.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.ZipDistance);
            lblZipSurcharge.Text = SessionVariable.Instance.UserDetails.Cost.ZipSurcharge < 0 ? "{Distance exceeds coverage area}" :
                string.Concat("$ ", Convert.ToString(SessionVariable.Instance.UserDetails.Cost.ZipDistance *
                SessionVariable.Instance.UserDetails.Cost.ZipSurcharge));

            lblTax.Text = Convert.ToString(SessionVariable.Instance.UserDetails.Cost.Tax);
            lblTaxDollars.Text = Convert.ToString(Convert.ToInt32(
                Utility.CalculateTotalTax(SessionVariable.Instance.UserDetails.Cost.Backsplash,
                SessionVariable.Instance.UserDetails.Cost.BacksplashPrice,
                SessionVariable.Instance.UserDetails.Cost.Edge,
                SessionVariable.Instance.UserDetails.Cost.EdgePrice,
                SessionVariable.Instance.UserDetails.Cost.Sinkout,
                SessionVariable.Instance.UserDetails.Cost.SinkoutPrice,
                SessionVariable.Instance.UserDetails.Cost.Surface,
                SessionVariable.Instance.UserDetails.Cost.SurfacePrice,
                SessionVariable.Instance.UserDetails.Cost.ZipDistance,
                SessionVariable.Instance.UserDetails.Cost.ZipSurcharge,
                SessionVariable.Instance.UserDetails.Cost.Tax)));
            
            CalculateTotalPrice();
            string s = SessionVariable.Instance.UserDetails.Cost.TotalPrice.ToString();
            s = string.Format("{0:C}", s);
            lblCost.Text = s;
        }
        
        private void CalculateTotalPrice()
        {
            double total = 0;
            
            total = Utility.CalculateTotalCostWithOutTax(SessionVariable.Instance.UserDetails.Cost.Backsplash,
                SessionVariable.Instance.UserDetails.Cost.BacksplashPrice,
                SessionVariable.Instance.UserDetails.Cost.Edge,
                SessionVariable.Instance.UserDetails.Cost.EdgePrice,
                SessionVariable.Instance.UserDetails.Cost.Sinkout,
                SessionVariable.Instance.UserDetails.Cost.SinkoutPrice,
                SessionVariable.Instance.UserDetails.Cost.Surface,
                SessionVariable.Instance.UserDetails.Cost.SurfacePrice,
                SessionVariable.Instance.UserDetails.Cost.ZipDistance,
                SessionVariable.Instance.UserDetails.Cost.ZipSurcharge);

            total = Utility.CalculateTotalCostWithTax(total, SessionVariable.Instance.UserDetails.Cost.Tax);
            SessionVariable.Instance.UserDetails.Cost.TotalPrice = Convert.ToInt32(total);
        }

        protected void rptrStoneCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Label lblStoneCategoryId = (Label)e.Item.FindControl("lblStoneCategoryId");
                Label lblStoneCategoryPrice = (Label)e.Item.FindControl("lblStoneCategoryPrice");

                if (lblStoneCategoryId != null && lblStoneCategoryPrice != null)
                {
                    int stoneCategoryId = Convert.ToInt32(lblStoneCategoryId.Text);

                    if (stoneCategoryId == SessionVariable.Instance.UserDetails.SelectedStoneCategoryId)
                    {
                        e.Item.Visible = false;
                    }
                    else
                    {
                        DAL.GetStoneTypeCategoryWithPriceScalarResult str = Common.GetStoneTypeCategoryWithPriceScalar(SessionVariable.Instance.UserDetails.SelectedStoneTypeId, stoneCategoryId);

                        if (str != null)
                        {
                            int startPrice = GetStoneCategoryEstimate(str.MinPrice);
                            int endPrice = GetStoneCategoryEstimate(str.MaxPrice);

                            lblStoneCategoryPrice.Text = String.Concat("$", startPrice, " - $", endPrice);
                        }
                    }
                }
            }
        }

        private int GetStoneCategoryEstimate(int surfacePrice)
        {
            double total = 0;

            total = Utility.CalculateTotalCostWithOutTax(
                SessionVariable.Instance.UserDetails.Cost.Backsplash,
                SessionVariable.Instance.UserDetails.Cost.BacksplashPrice,
                SessionVariable.Instance.UserDetails.Cost.Edge,
                SessionVariable.Instance.UserDetails.Cost.EdgePrice,
                SessionVariable.Instance.UserDetails.Cost.Sinkout,
                SessionVariable.Instance.UserDetails.Cost.SinkoutPrice,
                SessionVariable.Instance.UserDetails.Cost.Surface,
                surfacePrice,
                SessionVariable.Instance.UserDetails.Cost.ZipDistance,
                SessionVariable.Instance.UserDetails.Cost.ZipSurcharge);

            total = Utility.CalculateTotalCostWithTax(total, SessionVariable.Instance.UserDetails.Cost.Tax);
            return Convert.ToInt32(total);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectOptions.aspx");
        }

        protected void btnEmail_Click(object sender, EventArgs e)
        {
            if(SendEmail(SessionVariable.Instance.UserDetails.Email))
                {   
                    string sMsg = "We have sent this estimate quote to your email address " + SessionVariable.Instance.UserDetails.Email;
                    ShowMessage(sMsg);
                }
        }

        protected void btnSurface_Click(object sender, EventArgs e)
        {
            Response.Redirect("EnterShapeInputs.aspx");
        }

        protected void btnBackSplash_Click(object sender, EventArgs e)
        {
            Response.Redirect("EnterShapeInputs.aspx");
        }

        protected void btnEdge_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectEdge.aspx");
        }

        protected void btnSinkCut_Click(object sender, EventArgs e)
        {
            Response.Redirect("SinkOptions.aspx");
        }

        protected void btnEditStone_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectStoneCategory.aspx");
        }

        private void ShowMessage(string msgText)
        {
            HtmlGenericControl divMsg = (HtmlGenericControl)Page.Master.FindControl("divMsg");

            if (divMsg != null)
            {
                msg = this.Page.LoadControl("Controls/Message.ascx");

                if (msg != null)
                {
                    Label lblMessage = (Label)msg.FindControl("lblMessage");

                    if (lblMessage != null)
                    {
                        lblMessage.Text = msgText;
                    }

                    divMsg.Controls.Add(msg);
                }
            }

        }
    }
}